package com.prs.crm.action.crm;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.crm.CustomerTrade;
import com.prs.crm.service.crm.CustomerTradeService;
import com.prs.crm.service.sys.LogService;

public class CustomerTradeAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private CustomerTrade customerTrade;
	private CustomerTradeService customerTradeService;
	private PaginationSupport pagination;
	private Integer[] customerTradeIds;
	private Collection<TreeNode> nodes;
	private LogService logService;

	@Action("listCustomerTrade")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CustomerTrade.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCustomerTradeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("selectCustomerTrade")
	public String selectCustomerTrade() {
		return this.execute();
	}

	@Action("editCustomerTrade")
	public String edit() {
		if(this.customerTradeIds!=null){
			this.customerTrade=this.customerTradeService.get(customerTradeIds[0]);
		}
		return SUCCESS;
	}

	@Action(value = "savebtmTrade", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerTrade"),
			@Result(name = "error", type = "chain", location = "listCustomerTrade") })
	public String savebtmTrade() {
		this.customerTrade=this.customerTradeService.get(customerTradeIds[0]);
		customerTrade.setRemark("默认");
		this.customerTradeService.submit(customerTrade);
		return SUCCESS;
	}
	@Action(value = "saveqqTrade", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerTrade"),
			@Result(name = "error", type = "chain", location = "listCustomerTrade") })
	public String saveqq() {
		List<CustomerTrade> list=this.customerTradeService.getAllCustomerTrade();
		for(int i=0;i<list.size();i++){
			CustomerTrade tt=list.get(i);
			if(tt.getRemark()!=null){
				tt.setRemark("");
				this.customerTradeService.submit(tt);
			}
		}
		return SUCCESS;
	}
	@Action(value = "saveCustomerTrade", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerTrade"),
			@Result(name = "error", type = "chain", location = "editCustomerTrade") })
	public String save() {
		if (customerTrade.getName() == null
				|| "".equals(customerTrade.getName().trim())) {
			this.addActionError("行业名称不能为空");
			return ERROR;
		}
		if (this.getLogService().getSearchName(CustomerTrade.class, customerTrade
				.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getCustomerTradeService().save(customerTrade);
		return SUCCESS;
	}

	@Action(value = "removeCustomerTrade", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerTrade"),
			@Result(name = "error", type = "chain", location = "listCustomerTrade") })
	public String remove() {
		if (customerTradeIds == null) {
			this.addActionError("请选择要删除的客户行业");
			return ERROR;
		}
		for (Integer id : customerTradeIds) {
			CustomerTrade trade = this.getCustomerTradeService().get(id);
			if (trade.getCustomers().size() != 0) {
				this.addActionError("已有客户引用，不能删除");
				return ERROR;
			}
		}
		this.getCustomerTradeService().batchRemove(customerTradeIds);
		return SUCCESS;
	}

	@Action(value = "loadCustomerTradeTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadCustomerTradeTree() {
		nodes = new ArrayList<TreeNode>();
		Collection<CustomerTrade> trades = this.getCustomerTradeService()
				.getAllCustomerTrade();
		for (CustomerTrade trade : trades) {
			TreeNode node = new TreeNode();
			node.setId(trade.getId().toString());
			node.setText(trade.getName());
			nodes.add(node);
		}
		return SUCCESS;
	}

	public CustomerTrade getCustomerTrade() {
		return customerTrade;
	}

	public void setCustomerTrade(CustomerTrade customerTrade) {
		this.customerTrade = customerTrade;
	}

	public CustomerTradeService getCustomerTradeService() {
		return customerTradeService;
	}

	public void setCustomerTradeService(
			CustomerTradeService customerTradeService) {
		this.customerTradeService = customerTradeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getCustomerTradeIds() {
		return customerTradeIds;
	}

	public void setCustomerTradeIds(Integer[] customerTradeIds) {
		this.customerTradeIds = customerTradeIds;
	}

	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}

	public Collection<TreeNode> getNodes() {
		return nodes;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

}
